﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using RayD.Core;

namespace RayD.WebApi
{
    [ApiController]
    public abstract class BaseController : ControllerBase
    {
        #region 请求响应

        /// <summary>
        /// 返回成功消息
        /// </summary>
        /// <param name="msg">消息</param>
        /// <returns></returns>
        protected virtual ResParameter Success(string msg)
        {
            return new ResParameter { code = ResponseCode.success, msg = msg, data = new object { } };
        }
        protected virtual ResParameter<T> Success<T>(T data)
        {
            return new ResParameter<T> { code = ResponseCode.success, msg = "响应成功", data = data };
        }
        protected virtual ResParameter<T> Success<T>(string msg, T data)
        {
            return new ResParameter<T> { code = ResponseCode.success, msg = msg, data = data };
        }


        /// <summary>
        /// 返回失败消息
        /// </summary>
        /// <param name="msg">消息</param>
        /// <returns></returns>
        protected virtual ResParameter Fail(string msg)
        {
            //返回400状态码
            HttpContextHelper.Instance.Response.StatusCode = (int)ResponseCode.fail;
            return new ResParameter { code = ResponseCode.fail, msg = msg, data = new object { } };
        }
        protected virtual ResParameter<T> Fail<T>(T data)
        {
            //返回400状态码
            HttpContextHelper.Instance.Response.StatusCode = (int)ResponseCode.fail;
            return new ResParameter<T> { code = ResponseCode.fail, msg = "响应失败", data = data };
        }
        protected virtual ResParameter<T> Fail<T>(string msg, T data)
        {
            //返回400状态码
            HttpContextHelper.Instance.Response.StatusCode = (int)ResponseCode.fail;
            return new ResParameter<T> { code = ResponseCode.fail, msg = msg, data = data };
        }
        protected virtual ResParameter Fail(ResponseCode responseCode, string msg)
        {
            //返回400状态码
            HttpContextHelper.Instance.Response.StatusCode = (int)ResponseCode.fail;
            return new ResParameter { code = responseCode, msg = msg, data = new object { } };
        }

        #endregion
    }
}